package com.fzrskj.cloud.free.user.service;

import com.fzrskj.cloud.free.common.bean.ValidResult;
import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.CVerificationTool;
import com.fzrskj.cloud.free.user.bean.UserLog;
import com.fzrskj.cloud.free.user.utils.UserLogUtils;
import ms.core.consts.SysErr;
import ms.core.tool.XmlCodeParser;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class UserLogService {
	/**
	 * 根据条件查询操作日志
	 * @param req
	 * @param resp
	 * @param p={"userName":"userName","startTime":"startTime","endTime":"endTime","module":"module","page":page,"pagesize":pagesize}
	 * @param sid
	 * @return
	 */
	public String queryUserLog(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String userName = CJsonTool.getStrValue(p, "userName", "");
		String startTime = CJsonTool.getStrValue(p, "startTime", "");
		String endTime =   CJsonTool.getStrValue(p, "endTime", "");
		String module = CJsonTool.getStrValue(p, "module", "");
		String content =   CJsonTool.getStrValue(p, "content", "");
		int page = CJsonTool.getIntValue(p, "page", 1);
		if (page<1) {
			page = 1;
		}
		int pageSize = CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);
		if (pageSize<1) {
			pageSize = CConsts.PAGE_SIZE;
		}

		UserLogUtils userLogUtils = new UserLogUtils();
		List<UserLog> queryuserLog = userLogUtils.queryuserLog(userName, startTime, endTime, page, pageSize, module,content);
		String code = userLogUtils.getErrCode();

		int count = userLogUtils.queryuserLogCount(userName, startTime, endTime, module,content);
		int pages= Math.round((count+pageSize-1)/pageSize);
		return XmlCodeParser.formatResultPageData(code, page, pages, pageSize, count, CJsonTool.listBeanToJson(queryuserLog));
	}

	/**
	 * 查看操作日志详情
	 * @param req
	 * @param resp
	 * @param p={id:id}
	 * @param sid
	 * @return
	 */
	public String queryLogDetails(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		Long id = CJsonTool.getLongValue(p, "id", -1);
		if(id<0){
			return XmlCodeParser.formatResultTips(false,SysErr.SYS_PARAM_ERR);
		}
		UserLogUtils userLogUtils = new UserLogUtils();
		UserLog queryLogDetails = userLogUtils.queryLogDetails(id);
		String code = userLogUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS))
			return XmlCodeParser.formatResultTips(false, code);
		return XmlCodeParser.formatResultData(true, code, CJsonTool.beanToJson(queryLogDetails));
	}

	/**
	 * 根据条件删除操作日志
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 * @throws IOException 
	 */
	public String userLogDel(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		JSONObject j = CVerificationTool.StringToJsonObjectCV(p);
		ValidResult ret = CVerificationTool.verificationApi("userlogVf", j);
		if(!ret.isSuccess()){
			return XmlCodeParser.formatCustomTips(false, ret.getMsg());
		}
		UserLogUtils userLogUtils = new UserLogUtils();
		if(userLogUtils.deluserLog(j.optString("userName", ""), j.optString("startTime", ""),
			j.optString("endTime", ""), j.optString("module", ""), j.optInt("logTypeId", 0))>0){
			return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
		}else{
			return XmlCodeParser.formatResultTips(false,SysErr.SYS_DATA_NOT_FOUND);
		}
	}
}
